package com.cdy.jxc_project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdy.jxc_project.domain.TMenu;
import com.cdy.jxc_project.domain.TMenuRole;
import com.cdy.jxc_project.domain.TRole;
import com.cdy.jxc_project.json.R;
import com.cdy.jxc_project.mapper.TMenuRoleMapper;
import com.cdy.jxc_project.mapper.TRoleMapper;
import com.cdy.jxc_project.service.TRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/*
 * @Project:jxc_project
 * @Author:cdy(缘生)
 * @Motto:放下杂念,只为迎接明天更好的自己
 * @Date:2023/4/25 9:14
 * */
@Service
@Transactional
public class TRoleServiceImpl extends ServiceImpl<TRoleMapper, TRole> implements TRoleService {

    @Resource
    private TRoleMapper tRoleMapper;

    @Resource
    private TMenuRoleMapper tMenuRoleMapper;

    @Override
    public List<TRole> queryTRoleList() {
        return tRoleMapper.selectList(null);
    }

    @Override
    public TRole queryTRoleId(TRole tRole) {
        QueryWrapper<TRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name" , tRole.getName());
       queryWrapper.eq("name_zh",tRole.getNameZh());
        TRole tRoleGetId = tMenuRoleMapper.selectOne(queryWrapper);

        return tRoleGetId;
    }

    @Override
    public int deleteTRole(Integer id) {

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("rid", id);
        tMenuRoleMapper.delete(wrapper);
        return tRoleMapper.deleteById(id);
    }

    @Override
    public int updateTRole(TRole tRole) {
        return tRoleMapper.updateById(tRole);
    }

    @Override
    public int insertTRole(TRole tRole) {
        return tRoleMapper.insert(tRole);
    }

    @Override
    public R authToRole(Integer rid, List<Integer> mids) {
        // 删除原有权限
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("rid", rid);
        tMenuRoleMapper.delete(wrapper);
        if (CollectionUtils.isEmpty(mids)) {
            return R.error().message("角色id为空");
        } else {
            List<TMenuRole> list = new ArrayList<>();
            for (Integer mid : mids) {
                TMenuRole menuRole = new TMenuRole();
                menuRole.setRid(rid);
                menuRole.setMid(mid);
                list.add(menuRole);
            }
                /*
                第二种写法
                 */
//            List<TMenuRole> list = mids.stream().map(r -> {
//                TMenuRole role = new TMenuRole();
//                role.setRid(rid);
//                role.setMid(r);
//                return role;
//            }).collect(Collectors.toList());
            // 重新赋予权限
            Integer result = tMenuRoleMapper.insertBatch(list);
            return R.ok().message("角色授权成功");
        }



    }

    @Override
    public List<Integer> queryTRoleMenu(Integer id) {
        return tRoleMapper.queryTRoleMenu(id);
    }
}
